/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package kimo.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import kimo.persistence.entities.Producer;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import kimo.persistence.controllers.exceptions.IllegalOrphanException;
import kimo.persistence.controllers.exceptions.NonexistentEntityException;
import kimo.persistence.entities.UnitLocationType;

/**
 *
 * @author a381u296
 */
public class UnitLocationTypeJpaController implements Serializable {

    public UnitLocationTypeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(UnitLocationType unitLocationType) {
        if (unitLocationType.getProducerList() == null) {
            unitLocationType.setProducerList(new ArrayList<Producer>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Producer> attachedProducerList = new ArrayList<Producer>();
            for (Producer producerListProducerToAttach : unitLocationType.getProducerList()) {
                producerListProducerToAttach = em.getReference(producerListProducerToAttach.getClass(), producerListProducerToAttach.getProducerId());
                attachedProducerList.add(producerListProducerToAttach);
            }
            unitLocationType.setProducerList(attachedProducerList);
            em.persist(unitLocationType);
            for (Producer producerListProducer : unitLocationType.getProducerList()) {
                UnitLocationType oldProducerUnitLocationTypeIdOfProducerListProducer = producerListProducer.getProducerUnitLocationTypeId();
                producerListProducer.setProducerUnitLocationTypeId(unitLocationType);
                producerListProducer = em.merge(producerListProducer);
                if (oldProducerUnitLocationTypeIdOfProducerListProducer != null) {
                    oldProducerUnitLocationTypeIdOfProducerListProducer.getProducerList().remove(producerListProducer);
                    oldProducerUnitLocationTypeIdOfProducerListProducer = em.merge(oldProducerUnitLocationTypeIdOfProducerListProducer);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(UnitLocationType unitLocationType) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UnitLocationType persistentUnitLocationType = em.find(UnitLocationType.class, unitLocationType.getUnitLocationTypeId());
            List<Producer> producerListOld = persistentUnitLocationType.getProducerList();
            List<Producer> producerListNew = unitLocationType.getProducerList();
            List<String> illegalOrphanMessages = null;
            for (Producer producerListOldProducer : producerListOld) {
                if (!producerListNew.contains(producerListOldProducer)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Producer " + producerListOldProducer + " since its producerUnitLocationTypeId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Producer> attachedProducerListNew = new ArrayList<Producer>();
            for (Producer producerListNewProducerToAttach : producerListNew) {
                producerListNewProducerToAttach = em.getReference(producerListNewProducerToAttach.getClass(), producerListNewProducerToAttach.getProducerId());
                attachedProducerListNew.add(producerListNewProducerToAttach);
            }
            producerListNew = attachedProducerListNew;
            unitLocationType.setProducerList(producerListNew);
            unitLocationType = em.merge(unitLocationType);
            for (Producer producerListNewProducer : producerListNew) {
                if (!producerListOld.contains(producerListNewProducer)) {
                    UnitLocationType oldProducerUnitLocationTypeIdOfProducerListNewProducer = producerListNewProducer.getProducerUnitLocationTypeId();
                    producerListNewProducer.setProducerUnitLocationTypeId(unitLocationType);
                    producerListNewProducer = em.merge(producerListNewProducer);
                    if (oldProducerUnitLocationTypeIdOfProducerListNewProducer != null && !oldProducerUnitLocationTypeIdOfProducerListNewProducer.equals(unitLocationType)) {
                        oldProducerUnitLocationTypeIdOfProducerListNewProducer.getProducerList().remove(producerListNewProducer);
                        oldProducerUnitLocationTypeIdOfProducerListNewProducer = em.merge(oldProducerUnitLocationTypeIdOfProducerListNewProducer);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = unitLocationType.getUnitLocationTypeId();
                if (findUnitLocationType(id) == null) {
                    throw new NonexistentEntityException("The unitLocationType with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UnitLocationType unitLocationType;
            try {
                unitLocationType = em.getReference(UnitLocationType.class, id);
                unitLocationType.getUnitLocationTypeId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The unitLocationType with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Producer> producerListOrphanCheck = unitLocationType.getProducerList();
            for (Producer producerListOrphanCheckProducer : producerListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This UnitLocationType (" + unitLocationType + ") cannot be destroyed since the Producer " + producerListOrphanCheckProducer + " in its producerList field has a non-nullable producerUnitLocationTypeId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(unitLocationType);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<UnitLocationType> findUnitLocationTypeEntities() {
        return findUnitLocationTypeEntities(true, -1, -1);
    }

    public List<UnitLocationType> findUnitLocationTypeEntities(int maxResults, int firstResult) {
        return findUnitLocationTypeEntities(false, maxResults, firstResult);
    }

    private List<UnitLocationType> findUnitLocationTypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(UnitLocationType.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public UnitLocationType findUnitLocationType(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(UnitLocationType.class, id);
        } finally {
            em.close();
        }
    }

    public int getUnitLocationTypeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<UnitLocationType> rt = cq.from(UnitLocationType.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
